<?php
/**
 * Author: Silent Night
 * email: silentwolf_wp@163.com
 * Date: 2017/10/10
 * Time: 11:02
 */

namespace app\admin\model;

use think\Model;
use think;

class Sendwxmsg extends Model
{


    public function getID($pid)
    {
        $data = array();
        $res = $this->where(['id' => $pid])
            ->find();
        if ($res) {
            $data = $res->toArray();
        }
        return $data;
    }

    public function lists($w, $pagesize = "10")
    {
        $rdata = array();
        if (!empty($w))
            $this->where($w);
        $this->order("id", "desc");
        $list = $this->paginate($pagesize)->toArray();
//          echo $this->getLastSql();
        return $list;
    }

    public function getDataForWhere($w)
    {
        $data = array();
        $this->where($w);
        $this->order("id", "desc");
        $res = $this->find();
        if ($res) {
            $data = $res->toArray();
        }
        return $data;
    }

    public function getDataForWheres($w)
    {
        $data = array();
        $this->where($w);
        $this->order("id", "desc");
        $res = $this->select();

        if ($res) {
            $data = $res->toArray();
        }
        return $data;
    }


    public function getTopList($where, $num = 5)
    {
        $data = array();
        $this->where($where);
        $this->order("id", "desc");
        $res = $this->limit($num)->select();

        if ($res) {
            $data = $res->toArray();
        }
        return $data;
    }

    public function getLimit($num = 5)
    {
        $data = array();
        $this->where(array('is_send' => 0));
        $this->order("id", "asc");
        $res = $this->limit($num)->select();

        if ($res) {
            $data = $res->toArray();
        }
        return $data;
    }


    public function updateData($update, $id)
    {
        $res = $this->save($update, ['id' => $id]);
        return $res;
    }


    public function updateDataForWhere($update, $where)
    {
        $res = $this->save($update, $where);
        return $res;
    }


    public function add($array)
    {
        $this->save($array);
//                echo $this->getLastSql();
        return $this->getLastInsID();
    }


    public function delData($w)
    {

        return $this->where($w)->delete();
    }


    public function myMeetinglists($uid, $pagesize = "10", $page = 1)
    {

        $cdate = date("Y-m-d 00:00:00");

        $where = ' and  b.uid="' . $uid . '" and a.stime>"' . $cdate . '" and a.stats=1';
        $sql = 'select a.*,b.id as myid from p_meeting as a,p_meetingsign as b where a.id=b.mid ' . $where . ' order by a.stime asc';
        $allList = \think\Db::query($sql);
        $total = 0;
        $last_page = 0;
        if ($allList) {
            $total = count($total);
            $last_page = ceil($total / $pagesize);
        }
        $start = ($page - 1) * $pagesize;

        $sql = $sql . " limit " . $start . "," . $pagesize;
        $List = \think\Db::query($sql);

        $resultData = array(
            'total' => $total,
            'per_page' => $pagesize,
            'current_page' => $page,
            'last_page' => $last_page,
            'data' => $List,

        );
        return $resultData;
    }


    public function myHistoryMeetinglists($uid, $pagesize = "10", $page = 1)
    {

        $cdate = date("Y-m-d 00:00:00");

        $where = ' and  b.uid="' . $uid . '" and a.stime<="' . $cdate . '"  and a.stats=1';
        $sql = 'select a.*,b.id as myid from p_meeting as a,p_meetingsign as b where a.id=b.mid ' . $where . ' order by a.stime desc';
        $allList = \think\Db::query($sql);
        $total = 0;
        $last_page = 0;
        if ($allList) {
            $total = count($total);
            $last_page = ceil($total / $pagesize);
        }
        $start = ($page - 1) * $pagesize;

        $sql = $sql . " limit " . $start . "," . $pagesize;
        $List = \think\Db::query($sql);

        $resultData = array(
            'total' => $total,
            'per_page' => $pagesize,
            'current_page' => $page,
            'last_page' => $last_page,
            'data' => $List,

        );
        return $resultData;
    }

}